//
//  ItemDetailViewController.m
//  DanTang
//
//  Created by honeyrr on 2017/9/24.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "ItemDetailViewController.h"
#import "ItemDetailScrollView.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@interface ItemDetailViewController ()<UIScrollViewDelegate>

@property(nonatomic, strong) ItemDetailScrollView *scrollView;

@end

@implementation ItemDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"商品详情";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"PostItem_Share_18x22_"] style:UIBarButtonItemStyleDone target:self action:@selector(sharedBtnClick)];
    
    [self.view addSubview:self.scrollView];
}
//实现分享按钮的点击事件
-(void)sharedBtnClick
{
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"Me_AvatarPlaceholder_75x75_"],[UIImage imageNamed:@"Me_AvatarPlaceholder_75x75_"],[UIImage imageNamed:@"Me_AvatarPlaceholder_75x75_"],[UIImage imageNamed:@"Me_AvatarPlaceholder_75x75_"],[UIImage imageNamed:@"Me_AvatarPlaceholder_75x75_"]];
    NSArray *images = self.item[@"image_urls"];
    NSLog(@"images:%@",self.item);
    //（注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (images) {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        NSString *paramText = [NSString stringWithFormat:@"%@：%@",self.item[@"description"], self.item[@"purchase_url"]];
        [shareParams SSDKSetupShareParamsByText:paramText images:images url:[NSURL URLWithString:@"http://www.mob.com"] title:self.item[@"name"] type:SSDKContentTypeAuto];
        //有的平台要客户端分享需要加此方法，例如微博
        [shareParams SSDKEnableUseClientShare];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
        [ShareSDK showShareActionSheet:nil items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end){
            switch (state) {
                case SSDKResponseStateSuccess: {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail: {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
        }];
    }
}

-(ItemDetailScrollView *)scrollView
{
    if (!_scrollView) {
        ItemDetailScrollView *sv = [[ItemDetailScrollView alloc]initWithFrame:CGRectMake(0, 0, DTScreenW, DTScreenH)];
        sv.backgroundColor = [UIColor groupTableViewBackgroundColor];
        sv.delegate = self;
        sv.contentSize = CGSizeMake(DTScreenW, DTScreenH - 64 - 45 + 10 +520);
        _scrollView = sv;
    }
    return _scrollView;
}

-(void)setItem:(NSDictionary *)item
{
    _item = item;
    self.scrollView.item = item;
}


@end
